package command;

/**
 * @Author liangliangzhao
 * @data 2023/4/17 10:04
 */

public class RemoteController {
    Command[] onCommands;// 开按钮的命令数组
    Command[] offCommands;// 关按钮的命令数组
    Command undoCommand;// 执行撤销的命令
    public RemoteController() { //构造器 完成对按钮的初始化
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }
    public void setCommand(int no,Command onCommand,Command offCommand){//设置需要的命令
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }
    public void onButtonWasPushed(int no){//按下开的按钮
        onCommands[no].execute();//找到你按下的开的按钮,并调用对应的方法
        undoCommand = onCommands[no];//记录这次的操作 用于撤销
    }
    public void offButtonWasPushed(int no){//按下关的按钮
        offCommands[no].execute();//找到你按下的关的按钮,并调用对应的方法
        undoCommand = offCommands[no];//记录这次的操作 用于撤销
    }
    //按下撤销按钮
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}
